MACHINE LEARNING by Callaway Jason
Author:Callaway, Jason
Language: eng
Format: epub, pdf
Publisher: UNKNOWN
Published: 2021-10-16T16:00:00+00:00
Figure 6: Clusters assigned to the instances
We can plot the three clusters (0, 1, and 2) on the same scatter plot using the code below:
plt.figure(figsize=(16, 10))markers = ['x', '.', '+']
for i in range(0, 3): cond = df['Cluster'] == i df2 = df[cond] plt.scatter(df2['X1'], df2['X2'], label = 'Cluster '+str(i), marker = markers[i])
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], color = 'black', s = 50, marker = "s")
plt.xlabel('X1')plt.ylabel('X2')plt.legend()
Here, we first specify the size of our chart and declare a list called markers to store the markers for our clusters. Next, we use a for loop (that runs 3 times) to plot the clusters.
The first time the loop runs, i equals 0. The condition df['Cluster'] == i becomes df['Cluster'] == 0.
This condition selects rows in df with a Cluster value of 0. We assign the selected rows to a new DataFrame called df2 and use the scatter() function to plot a scatter plot for these rows, passing label = 'Cluster 0' and marker = 'x' to the function.
This plots all the data points in df with a Cluster value of 0, using 'x' as the marker.
After plotting the points for the first cluster, the for loop runs two more times to plot the other two clusters, changing the label and marker for each plot.
After plotting all three clusters, the for loop ends. Outside the for loop, we use the cluster_centers_ attribute to plot the centroids of our clusters.
cluster_centers_ stores the centroids as a NumPy array.
cluster_centers_[:, 0] gives us the first column of this array (i.e., the x-values for the centroids), while cluster_centers_[:, 1] gives us the second (i.e., the y-values).
If you run the code above, youâll get the ff. chart:
Figure 7: Scatter plot showing the different clusters and centroids
Clusters 0, 1, and 2 are plotted using 'x', '.', and '+', respectively, while the centroids are plotted using squares.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7705)
Robo-Advisor with Python by Aki Ranin(7603)
Offensive Shellcode from Scratch by Rishalin Pillay(6093)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(4997)
Ego Is the Enemy by Ryan Holiday(4953)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
Python for ArcGIS Pro by Silas Toms Bill Parker(4172)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3873)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3606)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3284)
Speed Up Your Python with Rust by Maxwell Flitton(3230)
Liar's Poker by Michael Lewis(3220)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3192)
Extreme DAX by Michiel Rozema & Henk Vlootman(3169)
Agile Security Operations by Hinne Hettema(3122)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3108)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3081)
Cryptography Algorithms by Massimo Bertaccini(3001)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2981)
